<?php

class ReportController extends MyClass_Action {
	protected $controller;
	protected $action;
	
	public function init() {
		
		$this->_user = $this->checkLogin ();
		
		/* Initialize action controller here */
		$this->controller = $this->getRequest ()->getControllerName ();
		$this->action = $this->getRequest ()->getActionName ();
		
		$this->commonTemplates ();
		
		$this->_smarty->assign ( 'controller', $this->controller );
		$this->_smarty->assign ( 'action', $this->action );
		$this->_smarty->assign ( '_user', $this->_user );

		$this->_smarty->assign ( 'title', 'Report' );
		
		//get database
		$this->getDb ( 'osmdb' );
		
					
	//verify rights
	//	if (isset ( $this->_user->uid ))
	//		$this->_redirect ( '/' );
	
	}
	
	public function indexAction() {
		try {
			//get datefrom and dateto
			$datefrom = $this->_request->getParam ( 'datefrom' );
			$dateto = $this->_request->getParam ( 'dateto' );
			$this->_smarty->assign ( 'datefrom', $datefrom );
			$this->_smarty->assign ( 'dateto', $dateto );			
			//print_r($datefrom);
			
			$report = array ();
			if (! empty ( $datefrom ) && ! empty ( $dateto )) {
				$sp = "CALL reportdata(".strtotime($datefrom).",".strtotime($dateto).")";
				$spResult = $this->_dbAdapter->fetchAll( $sp );
				if ($spResult) {
					foreach ( $spResult as $key => $value ) {
						if ($value['Report_Title'] == "TimeBandChart")
						{
							$sTimeBandChart = $value['Report_Value'];
							$ticksTimeBandChart = $value['Report_Key'];
							$this->_smarty->assign ( 'sTimeBandChart', $sTimeBandChart );
							$this->_smarty->assign ( 'ticksTimeBandChart', $ticksTimeBandChart );								
						}
						if ($value['Report_Title'] == "DayWeekChart")
						{
							$sDayWeekChart = $value['Report_Value'];
							$ticksDayWeekChartt = $value['Report_Key'];
							//print_r($sDayWeekChart); 
							//print_r($ticksDayWeekChartt); 						
							$this->_smarty->assign ( 'sDayWeekChart', $sDayWeekChart );
							$this->_smarty->assign ( 'ticksDayWeekChart', $ticksDayWeekChartt );								
						}			
						
						if ($value['Report_Title'] == "SourcesCallsChart")
						{
							$sSourcesCallsChart = $value['Report_Value'];
							$ticksSourcesCallsChart = $value['Report_Key'];
							//print_r($sSourcesCallsChart); 
							//print_r($ticksSourcesCallsChart); exit;
							$this->_smarty->assign ( 'sSourcesCallsChart', $sSourcesCallsChart );
							$this->_smarty->assign ( 'ticksSourcesCallsChart', $ticksSourcesCallsChart );								
						}							
					}
				}
			
				//print_r( $spResult ); exit;
			
			
				$where = 'and Contact_Date >= '.strtotime($datefrom).' and Contact_Date <= '.strtotime($dateto);
				
				//count callers
				$sql = "select 'No of Callers' as callerstitle, count(*) as numcallers from `arafmi_contact` where 1 " . $where;
				$result = $this->_dbAdapter->fetchAll ( $sql );
				if ($result) {
					foreach ( $result as $key => $value ) {
						$report ['callers'][$value['callerstitle']] = $value['numcallers'];
					}
				}
				//count gender
				$sql = 'select Client_Gender, count(*) as genderno from `arafmi_contact` where 1 ' . $where . ' group by Client_Gender';
				$result = $this->_dbAdapter->fetchAll ( $sql );
	
				if ($result) {
					foreach ( $result as $key => $value ) {
						if ($value['Client_Gender'] == 0)
						{
							$gendertitle = 'Male';
						}
						else
						{
							$gendertitle = 'Female';
						}
						$report ['gender'] [$gendertitle] = $value['genderno'];
					}
				}		
				
				//count Ethnicity
				$sql = 'select field_name, count(*) as ethnicityno from arafmi_contact c, arafmi_field f where 1 ' . $where . ' AND Client_Ethnicity = field_ID group by field_name';
			
				$result = $this->_dbAdapter->fetchAll ( $sql );
	
				if ($result) {
					foreach ( $result as $key => $value ) {
						$report ['ethnicity'] [$value['field_name']] = $value['ethnicityno'];
					}
				}	
				//Average Duration of Call
				$sql = "select 'Average Duration of Call' as durationtitle, avg(Contact_Duration) as avgduration from `arafmi_contact` where 1 " . $where;
			
				$result = $this->_dbAdapter->fetchAll ( $sql );
	
				if ($result) {
					foreach ( $result as $key => $value ) {
						$report ['duration'] [$value['durationtitle']] = $value['avgduration'];
					}
				}		
				//Time Band
				$sql = 'select Contact_Hour, count(*) as numcontact from `arafmi_contact` where 1 ' . $where . ' group by Contact_Hour';
			
				$result = $this->_dbAdapter->fetchAll ( $sql );
	
				if ($result) {
					foreach ( $result as $key => $value ) {
						$report ['timeband'] [$value['Contact_Hour']] = $value['numcontact'];
					}
				}					
				//print_r($report);
			}
			if ($spResult) {
				$this->_smarty->assign ( 'spResult', $spResult );			
			}
		
			//$xmlData = "<graph caption='Time of Calls Feb 2011' xAxisName='Time Band' yAxisName='Calls' showNames='1' decimalPrecision='0' formatNumberScale='0'><set name='9' value='0' color='AFD8F8' /><set name='10' value='6' color='F6BD0F' /><set name='11' value='6' color='8BBA00' /><set name='12' value='11' color='FF8E46' /><set name='13' value='11' color='008E8E' /><set name='14' value='9' color='D64646' /><set name='15' value='7' color='8E468E' /><set name='16' value='5' color='588526' /></graph>";
			//$s1 = "0, 6, 6, 11, 11, 9, 7, 5, 0";
			
			//$ticks = "'9', '10', '11', '12', '13', '14', '15', '16', '17'";
			//$ticks = "'M','T','W','Th','F'";
			$this->_smarty->assign ( 's1', $s1 );
			$this->_smarty->assign ( 'ticks', $ticks );			
			$this->_smarty->display ( $this->controller . '/' . $this->action . '.tpl' );
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'Attention', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	
	public function timeAction() {
		try {
			//user infomation
			
			if ($this->_user->branch) {
				$sql = 'select `branch_name` from `arafmi_branch` where `branch_id`=' . $this->_user->branch;
				$userinfo ['branch_name'] = $this->_dbAdapter->fetchOne ( $sql );
			} else
				$userinfo ['branch_name'] = 'admin';

			$this->_smarty->assign ( 'userinfo', $userinfo );
			
			
			$this->_smarty->display ( $this->controller . '/' . $this->action . '.tpl' );
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'Caution', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	public function normalAction() {
		try {
			$date = date('l jS \of F Y h:i:s A');
			
			$this->_smarty->assign ( 'date', $date );
			$this->_smarty->display ( $this->controller . '/' . $this->action . '.tpl' );
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'Attention', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//reserve statement as below
	public function noRouteAction() {
		$this->_redirect ( '/' );
	}
	
	public function __call($method, $args) {
		if ('Action' == substr ( $method, - 6 )) {
			$url = '/' . $this->controller . '/index';
			return $this->_redirect ( $url );
		}
		
		throw new Exception ( 'Invalid method' );
		
		// all other methods throw an exception
		throw new Exception ( 'Invalid method "' . $method . '" called' );
	}
}

